import { useUserStore } from '@/stores'
import { createRouter, createWebHistory } from 'vue-router'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { RouteName, RoutePath } from '@/enums/vue-router'

NProgress.configure({
  showSpinner: false,
})

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      name: RouteName.login,
      path: RoutePath['/login'],
      component: () => import('@/views/Login/index.vue'),
      meta: { title: '登录' },
    },
    {
      path: '/hehe',
      component: () => import('@/views/Layout/index.vue'),
      redirect: '/home',
      children: [
        {
          name: RouteName.home,
          path: RoutePath['/home'],
          component: () => import('@/views/Home/index.vue'),
          meta: { title: '首页' },
        },
        {
          name: RouteName.article,
          path: RoutePath['/article'],
          component: () => import('@/views/Article/index.vue'),
          meta: { title: '健康百科' },
        },
        {
          name: RouteName.notify,
          path: RoutePath['/notify'],
          component: () => import('@/views/Notify/index.vue'),
          meta: { title: '消息通知' },
        },
        {
          name: RouteName.user,
          path: RoutePath['/user'],
          component: () => import('@/views/User/index.vue'),
          meta: { title: '个人中心' },
        },
      ],
    },
    {
      name: RouteName.userPatient,
      path: RoutePath['/user/patient'],
      component: () => import('@/views/User/patient.vue'),
      meta: { title: '家庭档案' },
    },
    {
      name: RouteName.consultFast,
      path: RoutePath['/consult/fast'],
      component: () => import('@/views/Consult/ConsultFast.vue'),
      meta: { title: '极速问诊' },
    },
    {
      name: RouteName.consultDep,
      path: RoutePath['/consult/dep'],
      component: () => import('@/views/Consult/ConsultDep.vue'),
      meta: { title: '选择科室' },
    },
    {
      name: RouteName.consultIllness,
      path: RoutePath['/consult/illness'],
      component: () => import('@/views/Consult/ConsultIllness.vue'),
      meta: { title: '病情描述' },
    },
    {
      name: RouteName.consultPay,
      path: RoutePath['/consult/pay'],
      component: () => import('@/views/Consult/ConsultPay.vue'),
      meta: { title: '问诊支付' },
    },
    {
      name: RouteName.room,
      path: RoutePath['/room'],
      component: () => import('@/views/Room/index.vue'),
      meta: { title: '问诊室' },
      beforeEnter(to) {
        if (to.query.payResult === 'false') return '/user/consult'
      },
    },
    {
      name: RouteName.userConsult,
      path: RoutePath['/user/consult'],
      component: () => import('@/views/User/ConsultPage.vue'),
      meta: { title: '问诊记录' },
    },
    {
      name: RouteName.userConsultDetail,
      path: RoutePath['/user/consult/:id'],
      component: () => import('@/views/User/ConsultDetail.vue'),
      meta: { title: '问诊详情' },
    },
    {
      name: RouteName.orderPay,
      path: RoutePath['/order/pay'],
      component: () => import('@/views/Order/OrderPay.vue'),
      meta: { title: '药品支付' },
    },
    {
      name: RouteName.orderDetail,
      path: RoutePath['/order/:id'],
      component: () => import('@/views/Order/OrderDetail.vue'),
      meta: { title: '药品订单详情' },
    },
    {
      name: RouteName.orderPayResult,
      path: RoutePath['/order/pay/result'],
      component: () => import('@/views/Order/OrderPayResult.vue'),
      meta: { title: '药品支付结果' },
    },
    {
      name: RouteName.orderLogistics,
      path: RoutePath['/order/logistics/:id'],
      component: () => import('@/views/Order/OrderLogistics.vue'),
      meta: { title: '物流详情' },
    },
    {
      name: RouteName.loginCallback,
      path: RoutePath['/login/callback'],
      component: () => import('@/views/Login/LoginCallback.vue'),
      meta: { title: 'QQ登录-绑定手机' },
    },
  ],
})

// 访问权限控制
router.beforeEach((to) => {
  // 开启进度条
  NProgress.start()

  // 用户仓库
  const store = useUserStore()
  // 不需要登录的页面，白名单
  const whiteList = [RouteName.login, RouteName.loginCallback]
  // 如果没有登录且不在白名单内，去登录
  if (!store.user?.token && !whiteList.includes(to.name as RouteName))
    return { name: RouteName.login }
  // 否则不做任何处理
})

router.afterEach((to) => {
  document.title = `${import.meta.env.VITE_APP_TITLE}-${to.meta.title || ''}`
  NProgress.done()
})

export default router
